// Copyright 2011, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.twoscreen;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.channel.ChannelFailureException;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;

/**
 * This servlet creates the channel with the server and gets the token.
 */
@SuppressWarnings("serial")
public class TokenServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger("com.twoscreen.TokenServlet.java");

	private static ChannelService channelService = ChannelServiceFactory.getChannelService();

	/**
	 * Get the token for connecting & listening on the channel
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		log.info("Token request received");
		String userId = request.getParameter("userId");
		if (userId != null && !"".equals(userId)) {
			String token = createChannel(userId);
			writeIntoChannel(response,token);
		}
	}

	/**
	 * Creates the Channel token for the user 
	 * @param userId The User whom the token is created for  
	 * @return The token string is returned
	 */
	public String createChannel(String userId){
		try{
			log.info("Creating a channel for " + userId);
			return channelService.createChannel(userId);
		}catch(ChannelFailureException channelFailureException){
			log.warning("Error creating the channel");
			return channelService.createChannel(userId);
		}catch(Exception otherException){
			log.warning("Unknown exception while creating channel");
			return channelService.createChannel(userId);
		}
	}

	/**
	 * Writes the token in the response text 
	 * 
	 * @param response The response object to write the response text 
	 * @param token The token which needs to be written in the response
	 * @throws IOException
	 */
	public void writeIntoChannel(HttpServletResponse response, String token){
		try{
				log.info("Returning token to browser id: " + token);
				response.getWriter().print(token);
			} catch(IOException ioException){
				log.warning("Exception while writing to browser");
			} catch(Exception exception){
				log.warning("Unknow exception while writing to browser");
			}
	}
}
